
/*
 * nlock.c
 *
 *  Created on: 21/05/2012
 *      Author: Idan
 */
#include <pthread.h>
#include <assert.h>

typedef enum {READ, WRITE, MAYWRITE} lock_type;

typedef struct node_t {
	lock_type type;
	pthread_cond_t condition;
	pthread_t mayWriteId;
	int readers;
	struct node_t* next;
	int mayWriteFlag;
} node;

typedef struct lock_t {
	node* head;
	node* tail;
	int number_of_readers;
	int number_of_writers;
	pthread_mutex_t global_lock;
} nlock;


void lock_init(nlock* l);

static node* nodeInit(lock_type type);

void get_read_lock(nlock* l);

void get_may_write_lock(nlock *l);

void get_write_lock(nlock* l);

void upgrade_may_write_lock(nlock* l);

void release_shared_lock(nlock* l);

void release_exclusive_lock(nlock* l);

void lock_destroy(nlock* l);

